package com.xhwl.centiotdata.handler.device.impl;

import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.younijia.DeviceYouNiJiaDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.younijia.DeviceYouNiJiaHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author msy
 */
@Service
@Slf4j
public class DeviceIntegrationYouNiJiaHandler extends DeviceIntegrationHandler {

    @Autowired
    private DeviceYouNiJiaHandler deviceYouNiJiaHandler;

    @Autowired
    private IDeviceCentService deviceCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(ProductEnum.YOUNIJIA_PEDESTRIAN.getId(), this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDevice failed! deviceType is null!", configDTO.getProjectId());
            return;
        }

        if(DeviceTypeEnum.DOOR.getCode().equals(configDTO.getDeviceType())) {
            // 拉取数据
            List<DeviceYouNiJiaDTO> deviceYouNiJiaDTOList = deviceYouNiJiaHandler.pullPedestrianDevice(configDTO);
            if (CollectionUtils.isEmpty(deviceYouNiJiaDTOList)) {
                return;
            }
            // 转换数据
            List<DeviceDTO> deviceDTOList = deviceYouNiJiaHandler.handlePedestrianDevice(configDTO, deviceYouNiJiaDTOList);
            // 存储业务中台
            deviceCentService.asyncUpdateDevice(deviceDTOList);
        }

    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDeviceOnlineStatus failed! deviceType is null!", configDTO.getProjectId());
            return;
        }

        if(DeviceTypeEnum.DOOR.getCode().equals(configDTO.getDeviceType())) {
            // 拉取数据
            List<DeviceYouNiJiaDTO> deviceYouNiJiaDTOList = deviceYouNiJiaHandler.pullPedestrianDevice(configDTO);
            if (CollectionUtils.isEmpty(deviceYouNiJiaDTOList)) {
                return;
            }
            // 转换数据
            List<Device> deviceList = deviceYouNiJiaHandler.handlePedestrianDeviceOnlineStatus(configDTO, deviceYouNiJiaDTOList);
            // 存储业务中台
            deviceCentService.asyncUpdateOnlineStatus(deviceList);
        }
    }
}
